package com.rub.prajna.av.controller

import com.rub.prajna.av.view.VideoToGifView
import com.rub.prajna.core.AnimatedGif
import com.rub.prajna.core.Animation
import com.rub.prajna.core.video.VideoHandler
import javafx.collections.FXCollections
import javafx.embed.swing.SwingFXUtils
import javafx.event.ActionEvent
import javafx.fxml.FXML
import javafx.scene.control.Alert
import javafx.scene.control.ButtonType
import javafx.scene.image.Image
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.net.URL
import java.util.*
import javax.imageio.ImageIO


/**
 * https://stackoverflow.com/questions/31248983/how-to-create-an-integer-spinner-with-fxml
 * ?.表示当前对象如果为空则不执行，!!.表示当前对象如果为空也执行，然后会抛出空异常
 */
class VideoToGifController : VideoToGifView() {


    @FXML
    private fun onActionBtnOK(event: ActionEvent) {

        val cutTime = cutTime?.value
        val inputFile = inputFile?.text
        if (inputFile == null || "" == inputFile) {
            Alert(Alert.AlertType.ERROR, "请选择视频文件", ButtonType.OK).show()
            return
        }

        val startHourVal = startHour?.value
        val startMinuteVal = startMinute?.value
        val startSecondVal = startSecond?.value

        if (startHourVal == null || "" == startHourVal) {
            Alert(Alert.AlertType.ERROR, "请选择开始时间", ButtonType.OK).show()
            return
        }
        if (startMinuteVal == null || "" == startMinuteVal) {
            Alert(Alert.AlertType.ERROR, "请选择开始时间", ButtonType.OK).show()
            return
        }
        if (startSecondVal == null || "" == startSecondVal) {
            Alert(Alert.AlertType.ERROR, "请选择开始时间", ButtonType.OK).show()
            return
        }

        val startTime = "$startHourVal:$startMinuteVal:$startSecondVal"

        val videoHandler = VideoHandler(inputFile, cutTime, startTime).videoConvertGif()

        if (videoHandler.getStatus()) {
            outputFile?.text = videoHandler.getOutPutFile()
            playOrStop(videoHandler.getOutPutFile(), true)
        }

    }

    /**
     *
     */
    private fun playOrStop(outPutFile: String, isPlay: Boolean) {
        if (outPutFile == null || outPutFile == "") {
            return
        }

        val image: Image = SwingFXUtils.toFXImage(ImageIO.read(File(outPutFile)), null)
        this.imageView.image = image

        GlobalScope.launch {
            withContext(Dispatchers.IO) {
                //durationMs等于3秒
                val ani: Animation = AnimatedGif(outPutFile, 3000.0, imageView)
                ani.cycleCount = 10
                //播放、停止
                if (isPlay) ani.play() else ani.stop()
            }
        }

    }

    @FXML
    private fun onMouseEntered() {
        val outPutFile = outputFile?.text
        playOrStop(outPutFile, true)
    }

    @FXML
    private fun onMouseMoved() {
        val outPutFile = outputFile?.text
        playOrStop(outPutFile, false)
    }


    @FXML
    private fun onActionReset() {
        inputFile?.text = ""
        //clear selection value
        startHour.selectionModel.clearSelection()
        startMinute.selectionModel.clearSelection()
        startSecond.selectionModel.clearSelection()
    }

    override fun initialize(location: URL?, resources: ResourceBundle?) {
        startHour.items = FXCollections.observableArrayList(timeLists(13))
        startMinute.items = FXCollections.observableArrayList(timeLists(60))
        startSecond.items = FXCollections.observableArrayList(timeLists(60))
        //默认值选择第一个值
        startHour.selectionModel.selectFirst()
        startMinute.selectionModel.selectFirst()
        startSecond.selectionModel.selectFirst()
    }


}


